package com.blustreaksw.cyclecoach;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.blustreaksw.cyclecoach.R;
import com.blustreaksw.cyclecoach.data.WorkoutDatabaseHelper;

public class SplashScreenActivity extends Activity {

	private final static String TAG = "SplashScreenActivity";
    // Splash screen timer
    private static int SPLASH_TIME_OUT = 1500;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        
        if(WorkoutDatabaseHelper.getInstance(getApplicationContext()).isWorkoutActive(getApplicationContext()) == true) {
        	
        	long userId = WorkoutDatabaseHelper.getInstance(getApplicationContext()).getLastUser(getApplicationContext());
        	Intent i = new Intent(SplashScreenActivity.this, MainActivity.class);
    		i.putExtra(MainActivity.EXTRA_USER_ID, userId);
    		startActivity(i);
    		
    		// close this activity
            finish();
        } else {
 
	        new Handler().postDelayed(new Runnable() {
	 
	            /*
	             * Showing splash screen with a timer
	             */
	 
	            @Override
	            public void run() {
	                // This method will be executed once the timer is over
	            	long userId = WorkoutDatabaseHelper.getInstance(getApplicationContext()).getLastUser(getApplicationContext());
	            	Log.d(TAG, "Found userId " + userId);
	            	if(userId >= 0) {
	            		Intent i = new Intent(SplashScreenActivity.this, MainActivity.class);
	            		i.putExtra(MainActivity.EXTRA_USER_ID, userId);
	            		startActivity(i);
	            	} else {
	            		Intent i = new Intent(SplashScreenActivity.this, UserManagementActivity.class);
	            		startActivity(i);
	            	}
	 
	                // close this activity
	                finish();
	            }
	        }, SPLASH_TIME_OUT);
        }
    }
}
